package wstrader.task.admin;

import pc.system.*;
import wstrader.task.*;
import pc.sql.value.*;
//import java.sql.*;
import java.util.*;

public class Account  extends Role
{
  public int task_id = 0;
  public long user_id = 0;
  public int client_id = 0;
  public long id = 0;
  public Record user_ref = null;

  public Account(Tasks owner)
  {
    super(owner,"account");
  }

  public Tasks getTask(Tasks parent, String name)
  {
    if (parent == null) return null;
    return parent.getTask(name);
  }

  private Record getUser_Ref()
  {
    String sql =
        "SELECT us.* " +
        "FROM user_master us " +
        "WHERE us.id = " + this.user_id;
    if (this.user_ref != null) this.user_ref.close();
    return new Record(this.getSQLCommand().executeQuery(sql));
  }

  public int getPermission(String menu,int level)
  {
    String parent_menu = this.getRequest().getStringValue("menu" + (level-1)).trim();
    return this.getPermission(this.user_id,menu,parent_menu,level);
  }

  private long updateUserInfo()
  {
    Table tab = new Table(this,"user_master");
    tab.addParam("user_name");
    tab.addParam("full_name");
    tab.addParam("max_logon");
    tab.addParam("company_id");

    this.id = tab.update(this.id);

    String pw = this.getRequest().getStringValue("password").trim();
    String re = this.getRequest().getStringValue("re_password").trim();

    if (pw.length() == 0) return this.id;

    if (!pw.equals(re)) {
      this.getSysLog().setMessage("The password was not correctly confirmed.");
      return this.id;
    }
    String sql =
        "UPDATE user_master SET password = PASSWORD('" + pw + "') WHERE id = " + this.id;
    this.getSQLCommand().executeUpdate(sql);
    return this.id;
  }

  private void updateRight(int level, String parent_menu, String menu)
  {
    int perm = this.getRequest().getIntValue("permission");
    String sql =
        "UPDATE access_list SET permission = ? "  +
        "WHERE user_id = ? AND menu = ? AND (parent_menu = ? OR ?) AND (level = ?)";

    //System.out.println(sql);

    Vector vp = new Vector();
    vp.addElement(new IntValue(perm));
    vp.addElement(new LongValue(this.user_id));
    vp.addElement(new StringValue(menu));
    vp.addElement(new StringValue(parent_menu));
    vp.addElement(new BooleanValue(parent_menu == null));
    vp.addElement(new IntValue(level));

    if (this.getSQLCommand().executeUpdate(sql,vp) > 0) return;

    sql =
        "INSERT access_list " +
        "SET permission = ?, user_id = ?, menu = ?, parent_menu = ?, level = ?";

    vp.clear();
    vp.addElement(new IntValue(perm));
    vp.addElement(new LongValue(this.user_id));
    vp.addElement(new StringValue(menu));
    vp.addElement(new StringValue(parent_menu));
    vp.addElement(new IntValue(level));

    this.getSQLCommand().executeUpdate(sql,vp);
  }

  private Tasks getItem(Tasks parent,String parent_name, String child_name)
  {
    String pName = (parent.getName() == null) ? "" : parent.getName();
    String pname = (parent_name == null) ? "" : parent_name;
    String cname = (child_name == null)  ? "" : child_name;
    Tasks result = null;
    for (int i = 0; i < parent.getChildSize(); i++) {
      Tasks child = parent.getTask(i);
      String cName = (child.getName() == null)  ? "" : child.getName();
      if (pName.equals(pname) && cName.equals(cname)) {
        result = child;
        break;
      }
      result = getItem(child,parent_name,child_name);
      if (result != null) break;
    }
    return result;
  }

  private void updateAllChilds(Tasks parent,int level)
  {
    for (int i = 0; i < parent.getChildSize(); i++) {
      Tasks child = parent.getTask(i);
      this.updateRight(level,parent.getName(),child.getName());
      updateAllChilds(child,level+1);
    }
  }

  private void updateRights()
  {
    int level = this.getRequest().getIntValue("level");
    if (level <= 0) return;
    String parent_menu = this.getRequest().getStringValue("menu" + (level-1)).trim();
    if (parent_menu.length() == 0) parent_menu = null;
    String menu = this.getRequest().getStringValue("menu" + level).trim();

    Tasks root = this.getItem(this.getUser(),parent_menu,menu);
    if (root != null) {
      this.updateRight(level,parent_menu,menu);
      updateAllChilds(root,level+1);
    }
  }

  private void updateAllRights()
  {
    int level = this.getRequest().getIntValue("level");
    if (level <= 0) return;
    String parent_menu = this.getRequest().getStringValue("menu" + (level-1)).trim();
    if (parent_menu.length() == 0) parent_menu = null;
    if (parent_menu == null && level > 1) return;
    Tasks root = this.getUser();
    if (level == 2) root = this.getTask(root,parent_menu);
    if (level == 3) {
      root = this.getTask(root,"menu2");
      root = this.getTask(root, parent_menu);
    }
    updateAllChilds(root,level);
  }

  private void delete()
  {
    String sql = "DELETE FROM user_master WHERE id = " + this.user_id;
    this.getSQLCommand().executeUpdate(sql);
    sql = "DELETE FROM access_list WHERE user_id = " + this.user_id;
    this.getSQLCommand().executeUpdate(sql);
    this.user_id = 0;
  }

  public void doTask()
  {
    this.task_id = this.getRequest().getIntValue("task_id");
    this.client_id = this.getRequest().getIntValue("client_id");
    this.user_id = this.getRequest().getLongValue("user_id");
    switch (task_id) {
      case 1 : this.user_id = this.updateUserInfo();break;
      case 2 : this.updateRights();break;
      case 3 : this.updateAllRights(); break;
      case 4 : this.delete();break;
    }
    if (this.user_id == 0) this.user_id = 1;
    this.user_ref = this.getUser_Ref();
   }

}
